package com.ly.lypopupwindowdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.bean.SingleBean;

import java.util.List;

/**
 * Created by liying on 2017/12/28.
 */

public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.MyHolder> {

    private List<SingleBean> lists;


    public SingleAdapter(List<SingleBean> lists) {
        this.lists = lists;
    }

    @Override
    public SingleAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SingleAdapter.MyHolder holder, int position) {
        final SingleBean bean = lists.get(position);
        holder.singer.setText(bean.getSinger());
        holder.songname.setText(bean.getSongname());
        holder.radioButton.setSelected(bean.isSelected());

        holder.single_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (SingleBean b : lists) {
                    b.setSelected(false);
                }
                bean.setSelected(true);
                notifyDataSetChanged();

                Log.e("song", "点击" + bean.getSongname());
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        RelativeLayout single_rl;
        private ImageView radioButton;
        private TextView songname;
        private TextView singer;

        public MyHolder(View itemView) {
            super(itemView);
            radioButton = (ImageView) itemView.findViewById(R.id.ratioBtn);
            songname = (TextView) itemView.findViewById(R.id.songname);
            singer = (TextView) itemView.findViewById(R.id.singer);
            single_rl = (RelativeLayout) itemView.findViewById(R.id.single_rl);
        }
    }
}
